﻿using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Folder = OMC.DataManagement.Folder;
using MailContent = OMC.DataManagement.MailContent;
using System;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Diagnostics;

namespace OMC.Utils
{
    /// <summary>
    /// This is a static class and it supports methods to debug the Add-In.
    /// </summary>
    static class Debug
    {
        //Debug could be activated or disactivated
        private static bool enabled = off;
        private const bool on = true;
        private const bool off = false;
        private static LogWriter defaultWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

        /// <summary>
        /// The Method shows a Messagebox with Text: "--Here--".
        /// </summary>
        public static void PrintIfReachedCodeHere()
        {
            if (enabled)
            {
                MessageBox.Show("----HERE----");
            }
        }

        /// <summary>
        /// This method converts exception messages to a log file.
        /// </summary>
        /// <param name="message">The message to display.</param>
        /// <param name="ex">The exception</param>
        /// <param name="type">the TraceEventType, such as Information, Error, Critical</param>
        public static void Log(string message, Exception ex, TraceEventType type)
        {
            String exceptionMessage = ex.Message + "\n\n";
            String exceptionTrace = "StackTrace:\n\t" + ex.StackTrace.ToString();
            LogEntry logEntry = new LogEntry()
            {
                Severity = type,
                Title = message,
                Message = exceptionMessage + exceptionTrace
            };
            defaultWriter.Write(logEntry);
        }

        /// <summary>
        /// This method creates a Log Entry with a message and a severity Type.
        /// </summary>
        /// <param name="message">The message to display.</param>
        /// <param name="type">the severity type, such as Information, Error, Critical</param>
        public static void Log(string message, TraceEventType type)
        {
            LogEntry logEntry = new LogEntry()
            {
                Severity = type,
                Title = message,
            };
            defaultWriter.Write(logEntry);
        }

        /// <summary>
        /// This method creates a Log Entry with a message and sets the
        /// severity Type to TraceEventType.Information.
        /// </summary>
        /// <param name="message">The message to display.</param>
        public static void Log(string message)
        {
            LogEntry logEntry = new LogEntry()
            {
                Severity = TraceEventType.Information,
                Title = message,
            };
            defaultWriter.Write(logEntry);
        }

        /// <summary>
        /// The Method shows a MessageBox with a custom String.
        /// </summary>
        /// <param name="inString">custom String</param>
        public static void ShowMessageBox(string inString)
        {
            if (enabled)
            {
                MessageBox.Show(inString);
            }
        }

        /// <summary>
        /// The Method shows a messageBox with a String
        /// </summary>
        /// <param name="stringBuilder">a StringBuilder</param>
        public static void ShowMessageBox(StringBuilder stringBuilder)
        {
            if (enabled)
            {
                ShowMessageBox(stringBuilder.ToString());
            }
        }

        /// <summary>
        /// This Method prints a List.
        /// </summary>
        /// <param name="folderList">a list which contain objects of Folder Type</param>
        public static void ShowMessageBox(ref List<Folder> folderList)
        {
            if (enabled)
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendLine("Liste");
                foreach (Folder folder in folderList)
                {
                    stringBuilder.AppendFormat("*Folder: {0}, (Path: {1})\n",
                                               folder.FolderName,
                                               folder.FolderInfo);
                    foreach (MailContent mailContent in folder)
                    {
                        stringBuilder.AppendLine("\t\t <Mailcontent>");
                    }
                }
                ShowMessageBox(stringBuilder);
            }
        }
    }
}